﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Chapter6SuggestedPractice;

namespace Chapter6SuggestedPracticeApp
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		readonly MusicDatabasaeEntities _context = new MusicDatabasaeEntities("name=MusicDatabasaeEntities");

		public MainWindow()
		{
			InitializeComponent();
		}

		private void Window_Loaded(object sender, RoutedEventArgs e)
		{
			var albumViewSource = ((CollectionViewSource)(FindResource("albumViewSource")));
			// Load data by setting the CollectionViewSource.Source property:
			// albumViewSource.Source = [generic data source]
			var songViewSource = ((CollectionViewSource)(FindResource("songViewSource")));
			// Load data by setting the CollectionViewSource.Source property:
			// songViewSource.Source = [generic data source]
			albumViewSource.Source = GetAlbumQuery(_context).Execute(MergeOption.AppendOnly);
		}

		private void mnuSave_Click(object sender, RoutedEventArgs e)
		{
			try
			{
				_context.SaveChanges();
				MessageBox.Show("Changes saved");
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void mnuExit_Click(object sender, RoutedEventArgs e)
		{
			try
			{
				_context.SaveChanges();

			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			Application.Current.Shutdown();
		}

		private ObjectQuery<Album> GetAlbumQuery(MusicDatabasaeEntities context)
		{
			var albumsQuery = context.Albums;
			albumsQuery.Include("Songs");
			return albumsQuery;
		}
	}
}
